package HTTPSendIntegrator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class MmsPart {
	/** Identifier of the part*/
	private String partId;
	
	/** Part data */
	private byte[] partContent;
	
	/** Part content type*/
	private String partType;
	
	/** Charset of the text component */
	private String partCharset;
	
	// Creates an MmsPart object 

	public MmsPart(){
		this.partId = null;
		this.partContent = null;
		this.partType = MmsMessage.CTYPE_UNKNOWN;
		this.partCharset = MmsMessage.CHARSET_US_ASCII;
	}
	
	// Sets the part identifier.
	public void setPartId(String id){
		this.partId = id;
	}
	
	// Sets the part data.
	public void setPartContent(byte buffer[]){
		partContent = buffer;
	}
	
	//Sets the part data from a file.
	public void setPartContent(File f) throws FileNotFoundException, MmsContentException{
		RandomAccessFile file = new RandomAccessFile(f, "r");
		
		byte buf[] = null;
		try{
			long fileSize = file.length();
		
			if (fileSize > Integer.MAX_VALUE)
				throw new MmsContentException("MMS Part content file ("+ f +") too big.");
		
			buf = new byte[(int)fileSize];
			file.read(buf);
		}catch(IOException e){}
		
		setPartContent(buf);
	}
	
	/**
	 * Sets the part data from a file name.
	 * This implementation calls setPartContent(new File(filePath));
	 */
	public void setPartContent(String filePath) throws FileNotFoundException, MmsContentException{
		setPartContent(new File(filePath));
	}
	
	//Sets the part content type.
	public void setPartContentType(String contentType){
		this.partType = contentType;
	}
	
	// Returns the part identifier.
	public String getPartId(){
		if (this.partId == null) return "";
		else return this.partId;
	}
	
	// Returns the buffer containing the part data.
	public byte[] getPartContent(){
		return this.partContent;
	}
	
	// Returns the part content type.
	public String getPartContentType(){
		return this.partType;
	}
	
	// Returns the charset for the content.
	public String getPartCharset(){
		return this.partCharset;
	}
	
	// Sets the charset for the content.
	public void setPartCharset(String charset){
		this.partCharset = charset;
	}
}